package lk.icta.common.entity;

import org.hibernate.annotations.IndexColumn;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author Tharanga Punchihewa
 * @author Duminda Dharmakeerthi
 * @author Nimesh Karunathilaka
 */
@Entity
@Table(schema = "COMMON_RAIL")
@NamedQueries({
        @NamedQuery(
                name = "getPersonByName",
                query = "SELECT p FROM Person p WHERE p.name = :name"
        ),
        @NamedQuery(
                name = "getPersonByNIC",
                query = "SELECT p FROM Person p WHERE p.personNIC = :personNIC"
        ),
        @NamedQuery(
                name = "findAllPersons",
                query = "SELECT p FROM Person p"
        )
})
public class Person implements Serializable {
    /**
     * A system generated unique key
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    /**
     * Name of person
     */
    @Column(nullable = false)
    private String name;

    /**
     * NIC Number of the person
     */
    @Column(nullable = true)
    private String personNIC;

    /**
     * Nationality of the person is Sri Lankan or not
     */
    @Column(nullable = true)
    private boolean nationality;

    /**
     * Date of the birth of the person
     */
    @Column(nullable = true)
    @Temporal(value = TemporalType.DATE)
    private Date dateOfBirth;

    /**
     * Gender 0 - male, 1 - female, 2 - unknown
     */
    @Column(nullable = true)
    private int gender;

    /**
     * Title 0 - Mr., 1 - Mrs., 2 - Ms.
     */
    @Column(nullable = true)
    private int title;


    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
    @JoinColumn(nullable = true)
    private Contacts contacts;

    @Column(nullable = true)
    private String designation;

    // @ManyToMany(fetch = FetchType.EAGER)
    @OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
    @IndexColumn(name = "INDEX_COL")
    //TODO: use joincolum instead join table
    //@JoinColumn
    @JoinTable(schema = "COMMON_RAIL")
    private List<Child> children;


    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPersonNIC() {
        return personNIC;
    }

    public void setPersonNIC(String personNIC) {
        this.personNIC = personNIC;
    }

    public boolean isNationality() {
        return nationality;
    }

    public void setNationality(boolean nationality) {
        this.nationality = nationality;
    }

    public Date getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    public Contacts getContacts() {
        return contacts;
    }

    public void setContacts(Contacts contacts) {
        this.contacts = contacts;
    }

    public String getDesignation() {
        return designation;
    }

    public void setDesignation(String designation) {
        this.designation = designation;
    }

    public List<Child> getChildren() {
        return children;
    }

    public void setChildren(List<Child> children) {
        this.children = children;
    }

    public int getTitle() {
        return title;
    }

    public void setTitle(int title) {
        this.title = title;
    }
}
